extends PanelContainer

@onready var action_percent_label = %action_percent_label
@onready var action_percent_progressbar = %action_percent_progressbar
@onready var action_hour_percent_label = %action_hour_percent_label
@onready var action_hour_progressbar = %action_hour_progressbar
@onready var action_hour_percent_inner_label = %action_hour_percent_inner_label
@onready var task_num_label = %task_num_label
@onready var ability_num_label = %ability_num_label
@onready var task_container = %task_container
@onready var task_progressbar_scene = preload("res://components/taskl_progress_bar.tscn")
const ABILITY_PROGRESS_BAR = preload("res://components/ability_progress_bar.tscn")
@onready var ability_container = %ability_container
const ABILITY_PROGRESS_BAR_style = preload("res://assets/theme/ability_progress_bar.tres")
var today_finished_num = 0
var today_schedule_num = 0
var today_hours:float = 0
var today_hours_finished:float = 0
var today_task_num = 0
var today_ability_num = 0

func _ready():
	stats_home_infos()
	set_todays_info()
	show_all_task_progress()
	show_ability_hours()

func set_todays_info():
	_set_action_progress_percent()
	_set_action_hour_percent()
	_set_task_ability_num()
	
func _set_action_progress_percent():
	action_percent_label.text = '%d/%d' % [today_finished_num, today_schedule_num]
	action_percent_progressbar.value = int(float(today_finished_num)/today_schedule_num*100)

func _set_action_hour_percent():
	action_hour_percent_label.text = '%d/%d' % [today_hours_finished, today_hours]
	action_hour_percent_inner_label.text = str(int(float(today_hours_finished)/today_hours*100))+'%'
	action_hour_progressbar.value = int(float(today_hours_finished)/today_hours*100)

func _set_task_ability_num():
	task_num_label.text = str(today_task_num)
	ability_num_label.text = str(today_ability_num)

func stats_home_infos():
	var today = GlobalVariables.format_date(GlobalVariables.get_current_date())
	var period_schedules:Dictionary = GlobalVariables.date_period_schedules.get(today,{})
	# 统计今天的schedule信息
	var all_schedules_today:Array[String] = []
	for schedules in period_schedules.values():
		all_schedules_today.append_array(schedules)
	#today_schedule_num = all_schedules_today.size()
	
	var tasks = []
	var abilities = []
	for schedule_id in all_schedules_today:
	
		var schedule = GlobalVariables.get_schedule_data(schedule_id)
		if schedule.is_delayed:
			continue
			
		today_schedule_num += 1
			
		today_hours += schedule.length/float(60)
		
		if schedule.is_finished:
			today_finished_num +=1
			today_hours_finished += schedule.length/float(60)
			
		if schedule.related_task not in tasks:
			tasks.append(schedule.related_task)
			
		var related_task:TaskEntity = GlobalVariables.load_task(schedule.related_task)
		if related_task.related_ability not in abilities:
			abilities.append(related_task.related_ability)
	
	today_task_num = tasks.size()
	today_ability_num = abilities.size()


func show_all_task_progress():
	var tasks = GlobalVariables.load_all_tasks()
	if tasks.size()==0:
		var noTask = Label.new()
		noTask.text = '目前还没有进行中的任务，请赶紧创建吧'
		noTask.add_theme_color_override('font_color',Color.DARK_ORANGE)
		%task_vbcontainer.add_child(noTask)
		%task_container.hide()
		return
	
	tasks.sort_custom(GlobalUtil.sort_task)
	
	var index:int = 0
	for i in range(tasks.size()):
		var task = tasks[i]
		if not task.isNormal:
			continue
		
		if task.isFinished:
			continue
		
		index+=1
		add_task_info(index,task)

func add_task_info(index:int, taskEn:TaskEntity):
	#序号
	var index_label = Label.new()
	index_label.text = str(index)
	index_label.add_theme_font_size_override('font_size',14)
	task_container.add_child(index_label)
	# 任务名称
	var name_label = Label.new()
	name_label.text = taskEn.name
	name_label.add_theme_font_size_override('font_size',14)
	name_label.custom_minimum_size.x = 140
	name_label.clip_text = true
	name_label.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
	task_container.add_child(name_label)
	#任务的进度
	var task_progressbar = task_progressbar_scene.instantiate()
	task_progressbar.value = (taskEn.hours_used / float(taskEn.working_hours)*100)
	task_container.add_child(task_progressbar)
	#剩余时间
	var time_label = Label.new()
	time_label.text = '%d天后结束' % GlobalUtil.get_task_last_day_num(taskEn)
	time_label.add_theme_font_size_override('font_size',14)
	task_container.add_child(time_label)


func show_ability_hours():
	var abilities:Array = []
	var max_length:float = 0
	var total_hours_array = []
	for ability_id in GlobalVariables.get_all_ability_keys():
		var abilityEn = GlobalVariables.get_ability(ability_id)
		if abilityEn.parent_id != 'NULL':
			continue
		
		var total_hours = GlobalUtil.stat_ability_hours(abilityEn)
		total_hours_array.append(total_hours)
		if total_hours > max_length:
			max_length = total_hours
			
		abilities.append(abilityEn)
	var indexes = range(abilities.size())
	indexes.sort_custom(func(a,b) : return total_hours_array[a]>= total_hours_array[b])
	
	
	for i in range(abilities.size()):
		var index = indexes[i]
		add_ability_info(i+1,abilities[index],total_hours_array[index],max_length)
			
func compare_ability(a1:AbilityEntity, a2:AbilityEntity):
	return a1.hours>=a2.hours

func add_ability_info(index, abilityEn:AbilityEntity, hours_used:float, maxLength:float):
	#序号
	var index_label = Label.new()
	index_label.text = str(index)
	index_label.add_theme_font_size_override('font_size',14)
	ability_container.add_child(index_label)
	# 任务名称
	var name_label = Label.new()
	name_label.text = abilityEn.name
	name_label.add_theme_color_override('font_color',abilityEn.color)
	name_label.add_theme_font_size_override('font_size',14)
	ability_container.add_child(name_label)
	# 时间长度
	var ability_progressbar = ABILITY_PROGRESS_BAR.instantiate()
	var value = int(hours_used/maxLength*100)
	if value ==0:
		value = 1
	ability_progressbar.value = value
	var ability_style = ABILITY_PROGRESS_BAR_style.duplicate()
	ability_style.bg_color = abilityEn.color
	ability_progressbar.add_theme_stylebox_override('fill',ability_style)
	ability_container.add_child(ability_progressbar)
	#剩余时间
	var time_label = Label.new()
	time_label.text = '%d小时' % hours_used
	time_label.add_theme_font_size_override('font_size',14)
	ability_container.add_child(time_label)
		
	
	
			
	
	
